﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
// GetTPB, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
namespace GetTpb
{
    public static class TpbUtils
    {
        //
        public static DateTime FromPageDate2DT(string ss)
        {
            // Uploaded 37 mins ago, Size 157.86 MiB, ULed by hades09
            // Załadowany Dzisiaj 16:05, Rozmiar 215.81 MiB, Dodany przez sigchill 
            //11-14 2007
            ss = ss.Trim();
            //
            if (ss.SW("Uploaded ")) ss = ss.Substring("Uploaded ".Length);
            if (ss.SW("Załadowany ")) ss = ss.Substring("Załadowany ".Length);
            ss = ss.Trim();
            //
            if ("" == ss) return DateTime.Now;
            //
            DateTime dt = DateTime.Now;
            //
            //
            //--[28 min. temu]
            if (ss.EW("min. temu") || ss.EW("mins ago"))
            {
                double m = 0.0;
                try
                {
                    ss = ss.Remove(ss.Length - 9).Trim();
                    m = double.Parse(ss);
                    return DateTime.Now.Subtract(TimeSpan.FromMinutes(m));
                }
                catch (Exception e)
                {
                    System.Windows.MessageBox.Show(e.Message, "[" + ss + "]");
                    return DateTime.Now;
                }
            }
            //
            //
            //--[Dzisiaj]
            if (ss.SW("Dzisiaj") || ss.SW("Today"))
            {
                int h = 0, m = 0;
                try
                {
                    ss = ss.Substring(7).Trim();
                    string[] hm = ss.Split(':');
                    h = int.Parse(hm[0]);
                    m = int.Parse(hm[1]);
                    return new DateTime(dt.Year, dt.Month, dt.Day, h, m, 0);
                }
                catch (Exception e)
                {
                    System.Windows.MessageBox.Show(e.Message, "[" + ss + "]");
                    return DateTime.Now;
                }
            }
            //
            //
            //--[Wczoraj]
            if (ss.SW("Wczoraj") || ss.SW("Y-day"))
            {
                try
                {
                    dt = DateTime.Now.Subtract(TimeSpan.FromDays(1.0));
                    int h = 0, m = 0;
                    ss = ss.Substring(7).Trim();
                    string[] hm = ss.Split(':');
                    h = int.Parse(hm[0]);
                    m = int.Parse(hm[1]);
                    return new DateTime(dt.Year, dt.Month, dt.Day, h, m, 0);
                }
                catch (Exception e)
                {
                    System.Windows.MessageBox.Show(e.Message, "[" + ss + "]");
                    return DateTime.Now;
                }
            }
            //
            string[] s2 = ss.Split(' ');
            //
            if (2 != s2.Count())
            {
                return DateTime.Now;
            }
            //
            if (s2[1].Contains(":"))
            {
                //
                //--[09-01 12:35]
                try
                {
                    int M = 0, D = 0;
                    int h = 0, m = 0;
                    string[] M_D = s2[0].Split('-');
                    string[] h_m = s2[1].Split(':');
                    M = int.Parse(M_D[0].Trim());
                    D = int.Parse(M_D[1].Trim());
                    h = int.Parse(h_m[0].Trim());
                    m = int.Parse(h_m[1].Trim());
                    return new DateTime(dt.Year, M, D, h, m, 0);
                }
                catch (Exception e)
                {
                    System.Windows.MessageBox.Show(e.Message, "[" + ss + "]");
                    return DateTime.Now;
                }
                //
            }
            else
            {
                //
                //--[08-21 2004]
                try
                {
                    int M = 0, D = 0, Y = 0;
                    string[] M_D = s2[0].Split('-');
                    M = int.Parse(M_D[0].Trim());
                    D = int.Parse(M_D[1].Trim());
                    Y = int.Parse(s2[1].Trim());
                    return new DateTime(Y, M, D, 0, 0, 0);
                }
                catch (Exception e)
                {
                    System.Windows.MessageBox.Show(e.Message, "[" + ss + "]");
                    return DateTime.Now;
                }
                //
            }
            //
            return DateTime.Now;
        }
        //
        //
        public static double FromPageSize2Dbl(string ss)
        {
            ss = ss.Trim();
            if (ss.SW("Rozmiar ")) ss = ss.Substring("Rozmiar ".Length);
            if (ss.SW("Size ")) ss = ss.Substring("Size ".Length);
            //
            ss = ss.Trim();
            if ("" == ss) return 0;
            //
            try
            {
                if (ss.EW(" MiB"))
                {
                    double n = 0.0;
                    ss = ss.Substring(0, ss.Length - 4);
                    ss = ss.Replace('.', ',');
                    if (double.TryParse(ss, out n))
                    {
                        return n;
                    }
                    return 0.0;
                }
                if (ss.EW(" GiB"))
                {
                    double n = 0.0;
                    ss = ss.Substring(0, ss.Length - 4);
                    ss = ss.Replace('.', ',');
                    if (double.TryParse(ss, out n))
                    {
                        return 1000.0 * n;
                    }
                    return 0.0;
                }
            }
            catch (Exception e)
            {
                //System.Windows.Forms.MessageBox.Show(e.Message, "FromPageSize2Dbl");
                System.Windows.MessageBox.Show(e.Message, "FromPageSize2Dbl");
            }
            //
            return 0.0;
        }
        //
        public static int FromPage2Int(string s)
        {
            int n;
            if (int.TryParse(s, out n))
            {
                return n;
            }
            return -1;
        }
        //
        public static string HashSet2String(string title, HashSet<TpbItem> hs)
        {
            string s = TpbItem.GetHtmlBegin(title);
            foreach (TpbItem ti in hs)
            {
                s += ti.GetHtmlTableRow();
            }
            s += TpbItem.GetHtmlEnd();
            //
            return s;
        }
        //
        public static string Error2String(string title, string msg)
        {
            string s = "";
            s += "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">";
            s += "<html>";
            s += "<head>";
            s += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>";
            s += "<title>" + title + "</title>";
            s += "<body>";
            s += string.Format("<h2>{0}</h2>", msg);
            s += "</body>";
            s += "</html>";
            //
            return s;
        }
        //
    }//class
}//namespace
